//
//  TestViewController_2.swift
//  MZTool_Example
//
//  Created by gby on 2023/2/13.
//  Copyright © 2023 CocoaPods. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import RxRelay
import MZTool
//import FDFullscreenPopGesture

class TestViewController_2: BaseViewVCtrl  {
    
    let disposebag = DisposeBag()
    
    var obser1: Observable<String>?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationController?.navigationBar.isHidden = true
        self.mz_prefersNavigationBarHidden = true
//        self.fd_prefersNavigationBarHidden = true
//        let interval = Observable<Int>.interval(RxTimeInterval.seconds(1), scheduler: MainScheduler.instance).publish().refCount()
//        _ = interval.subscribe(onNext: { print("订阅1: \($0)") })
//        DispatchQueue.main.asyncAfter(deadline: 5) {
//            _ = interval.subscribe(onNext: { print("订阅2: \($0)") })
//        }


//        let sequenceThatFails = PublishSubject<String>()
//        let recoverySequence1 = Observable.of("11", "22", "33")
//        let recoverySequence2 = Observable.of("44", "55", "66")
//
//        sequenceThatFails.catch {
//            print("Error:", $0)
//            return recoverySequence1
//        }.subscribe(onNext: { print($0) }).disposed(by: disposebag)
//
//
//        sequenceThatFails.onNext("1")
//        sequenceThatFails.onNext("2")
//        sequenceThatFails.onNext("3")
//        sequenceThatFails.onError(NSError.init(domain: "111", code: 1))
//        sequenceThatFails.onNext("4")
//        sequenceThatFails.onNext("5")
//        sequenceThatFails.onNext("6")
//        sequenceThatFails.onError(NSError.init(domain: "222", code: 2))
//        sequenceThatFails.onNext("7")
//        sequenceThatFails.onNext("8")
//        sequenceThatFails.onNext("9")
        
        let b = BehaviorRelay<String>(value: "")
        b.asObservable().subscribe(onNext: {
            print("\($0)")
        }).disposed(by: disposebag)
        b.accept("1")
        b.accept("2")
        DispatchQueue.main.mz.after(5) {
            b.accept("3")
        }
        
        
        do {
            let btn = UIButton.init(type: .custom)
            btn.setTitle("1111", for: .normal)
            btn.setTitleColor(.black, for: .normal)
            btn.rx.tap.subscribe { event in
                self.action1()
            }.disposed(by: disposebag)
            btn.frame = CGRect(x: 50, y: 200, width: 100, height: 30)
            self.view.addSubview(btn)
            
            var count = 1
            
            obser1 = Observable<String>.create { observer in
                observer.onNext("11")
                observer.onNext("11")
                
                if count == 1 {
                    observer.onError(NSError.init(domain: "12321312", code: 1))
                    count += 1
                }
                
                observer.onNext("done")
                observer.onCompleted()
                
                return Disposables.create()
            }
        }
        
        do {
            let btn = UIButton.init(type: .custom)
            btn.setTitle("xxxxxx", for: .normal)
            btn.setTitleColor(.black, for: .normal)
            btn.rx.tap.subscribe { event in
                self.action3()
            }.disposed(by: disposebag)
            btn.frame = CGRect(x: 50, y: 250, width: 100, height: 30)
            self.view.addSubview(btn)
            
//            UITextField.rx.text
            
//            let result = UITextField.init().rx.text.asDriver().throttle(RxTimeInterval.milliseconds(300)).flatMapLatest { _ in
//                return
//            }
        }
    }
    
    func action1() {
        MZ.printLog("\(RxSwift.Resources.total)")
        obser1?.debug().retry(2).subscribe(onNext: { MZ.printLog($0) }).disposed(by: disposebag)
        MZ.printLog("\(RxSwift.Resources.total)")
    }
    
    
    func action2() {
        
//        @Sendable func noAwaitAsynclet() async {
//             print("begin noAwaitAsynclet")
//             try? await Task.sleep(nanoseconds: 10_000_000_000)
//            Task.isCancelled ? print("noAwaitAsynclet is cancelled") : print("end noAwaitAsynclet")
//           }
//
//           func testAsynclet() async {
//             let parentTask =
//            Task {
//              async let test = noAwaitAsynclet()
//                await test
//            }
//
//               parentTask.cancel()
//               await parentTask.value
//            print("parentTask finished!")
//          }
//
//        Task{
//            await testAsynclet()
//        }
        
//        func aaa2() async -> [Int] {
//            await withTaskGroup(of: Int.self, body: { group in
//                var ary:[Int] = []
//                group.addTask {
//                    await noAwaitAsynclet()
//                    return 1
//                }
//                group.addTask {
//                    await noAwaitAsynclet()
//                    return 2
//                }
//                for await index in group {
//                    ary.append(index)
//                }
//                return ary
//            })
//        }
//
//
//        Task{
////            let v = await aaa2()
////            print(v)
//            let task = Task {
//                print("task start")
//                try await Task.sleep(nanoseconds: 10_000_000_000)
//                try Task.checkCancellation()
//                print("task finish, isCancelled: \(Task.isCancelled)")
//            }
//            try await Task.sleep(nanoseconds: 5_000_000_000)
//            task.cancel()
//            print(await task.result)
//
//        }
//
//        let bank = BankAccount(accountNumber: 1, initialDeposit: 2)
//        print(bank.accountNumber)
//        Task{
//            await bank.changebalance(banlance:1)
//        }
    }
    
    func action3() {
        let vc = TestViewController.init();
        self.navigationController?.pushViewController(vc, animated: true)
    }
}


//actor BankAccount {
//    let accountNumber: Int
//    var balance: Double
//    var dddddd: String = ""
//
//    init(accountNumber: Int, initialDeposit: Double) {
//        self.accountNumber = accountNumber
//        self.balance = initialDeposit
//    }
//    
//    func changebalance(banlance: Double) async {
//        self.balance = banlance
//    }
//}
